<template>
  <div class="student_model_wrap">
    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学员姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学员姓名" />
        </el-form-item>
        <el-form-item label="学员性别" prop="studentSex">
          <el-select v-model="form.studentSex" placeholder="请选择学员性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="头像">
          <imageUpload v-model="form.headUrl" :limit="1"/>
        </el-form-item>
        <el-form-item label="出生日期" prop="studentBirthday">
          <el-date-picker
            v-model="form.studentBirthday"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出生日期"
          />
        </el-form-item>
        <el-form-item label="在读学校" prop="atSchool">
          <el-input v-model="form.atSchool" placeholder="请输入在读学校" />
        </el-form-item>
        <el-form-item label="家长姓名" prop="parentName">
          <el-input v-model="form.parentName" placeholder="请输入家长姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="studentMobile">
          <el-input v-model="form.studentMobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="联系电话" prop="studentMobile2">
          <el-input v-model="form.studentMobile2" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="家庭地址" prop="studentAddress">
          <el-input v-model="form.studentAddress" placeholder="请输入家庭地址" />
        </el-form-item>
        <el-form-item label="QQ或微信">
          <el-input v-model="form.studentNumber" placeholder="请输入QQ或微信" />
        </el-form-item>
        <el-form-item label="学生状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择学生状态">
            <el-option
              v-for="dict in dict.type.sys_student_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="就读班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addStudent, updateStudent, listClass } from '@/api/school/student'
export default {
  dicts: ['sys_user_sex','sys_student_type'],
  a: { name: '张三' },
  data() {
    return {
      title: '',
      open: false,
      classList: null, // 班级列表
      // 表单参数
      form: {
        id: null,
        studentNumber: null,
        studentName: null,
        studentSex: null,
        studentBirthday: null,
        studentAddress: null,
        studentMobile: null,
        atSchool: null,
        openId: null,
        parentName: null,
        headUrl: null,
        createTime: null,
        updateTime: null,
        classId: null
      },
      // 表单校验
      rules: {
        studentName: [
          { required: true, message: '请输入学员姓名', trigger: 'blur' }
        ],
        studentSex: [
          { required: true, message: '请选择学员性别', trigger: 'change' }
        ],
        studentBirthday: [
          { required: true, message: '请选择出生日期', trigger: 'blur' }
        ],
        atSchool: [
          { required: true, message: '请输入在读学校', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '请输入家长姓名', trigger: 'blur' }
        ],
        studentMobile: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        studentAddress: [
          { required: true, message: '请输入家庭地址', trigger: 'blur' }
        ],
        // studentNumber: [
        //   { required: true, message: '请输入QQ或微信', trigger: 'blur' }
        // ],  
        state: [
          { required: true, message: '请选择学生状态', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getListClass() {
      listClass({}).then(res => {
        this.classList = res.data
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$emit('updatedata')
            })
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.$emit('updatedata')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentNumber: null,
        studentName: null,
        studentSex: null,
        studentBirthday: null,
        studentAddress: null,
        studentMobile: null,
        atSchool: null,
        openId: null,
        parentName: null,
        headUrl: null,
        createTime: null,
        updateTime: null,
        classId: null
      }
      this.classList = null;
      this.resetForm('form')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
